package team.archai.nexus.boot.platform.modules.system.endpoint;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import team.archai.nexus.boot.platform.basics.endpoint.BasicsEndPoint;
import team.archai.nexus.boot.platform.core.tool.TreeBuilder;
import team.archai.nexus.boot.platform.modules.system.dto.MenuDTO;
import team.archai.nexus.boot.platform.modules.system.entity.Menu;
import team.archai.nexus.boot.platform.modules.system.service.IMenuService;
import team.archai.nexus.boot.platform.modules.system.vo.RouterMenuTree;
import team.archai.nexus.boot.web.response.ResponseModel;
import team.archai.nexus.boot.web.response.ResultUtil;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * <p>
 * 系统菜单 控制器
 * </p>
 *
 * @author shibin
 * @since 2023-05-20
 */
@RestController
@RequestMapping("/basics-system/menu")
@Tag(name = "系统菜单", description = "系统菜单接口")
public class MenuEndpoint extends BasicsEndPoint<IMenuService, MenuDTO, Menu> {


    public Function<Menu, RouterMenuTree> entity2MenuTree = entity -> {
        RouterMenuTree menuTree = new RouterMenuTree();
        menuTree.setId(entity.getId());
        menuTree.setName(entity.getMenuName());
        menuTree.setCategory(entity.getMenuType());
        menuTree.setComponent(entity.getComponent());
        menuTree.setPath(entity.getRouterPath());
        menuTree.setParentId(entity.getParentId());
        RouterMenuTree.Meta meta = new RouterMenuTree.Meta();
        meta.setIcon(entity.getMenuIcon());
        meta.setTitle(entity.getMenuName());
        meta.setCode(entity.getMenuCode());
        meta.setLink(entity.getRouterPath());
        menuTree.setMeta(meta);
        return menuTree;
    };

    @GetMapping("/routers")
    @Operation(summary = "获取菜单树形集合", description = "获取菜单树形集合")
    protected ResponseModel<List<RouterMenuTree>> routers() {
        List<Menu> entityList = service.list();
        List<RouterMenuTree> treeList = entityList.stream().map(entity2MenuTree).collect(Collectors.toList());
        List<RouterMenuTree> trees = TreeBuilder.buildList(treeList);
        return ResultUtil.success(trees);
    }
}
